<% content_for :css do %>
  <link rel="stylesheet" type="text/css" href="/css/webuploader.css">
  <style>
    div.webuploader-pick {
      padding: 0 0;
      width: 80px;
      height: 80px;
    }
    p.state {
      position: relative;
      top: -20px;
    }
  </style>
<% end %>
<section class="wrapper">
  <!-- page start-->
  <div class="row">
    <div class="col-lg-12">
      <section class="panel">
        <header class="panel-heading">
          修改信息
        </header>
        <div class="panel-body">
          <form class="form-horizontal adminex-form" >
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>上传头像</label>
              <div class="col-sm-10" >
                <div id="upload_pic" title="点击上传">
                  <img src="/uploads/users/<%= @user.user_pic %>" style="width: 80px;height: 80px;" alt="点击上传">
                  <p class="state"></p>
                  <input type="text" name="random_name" value="<%= @user.user_pic %>" hidden>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>学号</label>
              <div class="col-sm-10">
                <input type="number" disabled value="<%= @user.student_no %>" class="form-control" placeholder="请输入学号">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"><i class="bi_tian">*&nbsp</i>姓名</label>
              <div class="col-sm-10">
                <input type="text" name="name" value="<%= @user.name %>" class="form-control" placeholder="请输入姓名">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">性别</label>
              <div class="col-sm-10">
                <select class="form-control" name="sex">
                  <option value="1">男</option>
                  <option value="2" <%= @user.sex == 2 ? 'selected' : '' %>>女</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">电话</label>
              <div class="col-sm-10">
                <input type="number" name="phone" value="<%= @user.phone %>" class="form-control" placeholder="请输入数字">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">学院</label>
              <div class="col-sm-10">
                <select class="form-control" name="academy_id">
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">专业</label>
              <div class="col-sm-10">
                <select class="form-control" name="major_id">
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">邮箱</label>
              <div class="col-sm-10">
                <input type="email" name="mail" value="<%= @user.mail %>" class="form-control" placeholder="请输入邮箱">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">宿舍地址</label>
              <div class="col-sm-10">
                <input type="text" name="location" value="<%= @user.location %>" class="form-control" placeholder="请输入地址">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">籍贯</label>
              <div class="col-sm-10">
                <input type="text" name="native_place" value="<%= @user.native_place %>" class="form-control" placeholder="请输入籍贯">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">紧急联系人姓名</label>
              <div class="col-sm-10">
                <input type="text" name="emer_name" value="<%= @user.emer_name %>" class="form-control" placeholder="请输入紧急联系人姓名">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">紧急联系人电话</label>
              <div class="col-sm-10">
                <input type="number" name="emer_phone" value="<%= @user.emer_phone %>" class="form-control" placeholder="请输入紧急联系人电话">
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label"></label>
              <div class="col-sm-10">
                <button type="button" class="btn btn-primary" id="submit_data" data-url="/admin/my/edit">提交</button>
              </div>
            </div>
          </form>
        </div>
      </section>
    </div>
  </div>
  <!-- page end-->
</section>
<% content_for :js do %>
  <script type="text/javascript" src="/js/webuploader.js"></script>
  <script type="text/javascript" src="/js/upload_pic.js"></script>
  <script type="text/javascript" charset="utf-8">
    $(function () {
        uploader.option( 'server','/admin/my/upload');
    });
      $('#submit_data').click(function () {
          var _this            = $(this);
          var url              = _this.attr('data-url');
          var data             = {};
          data['user_pic']     = $('input[name=random_name]').val();
          data['name']         = $('input[name=name]').val();
          data['sex']          = $('select[name=sex]').val();
          data['phone']        = $('input[name=phone]').val();
          data['academy_id']   = $('select[name=academy_id]').val();
          data['major_id']     = $('select[name=major_id]').val();
          data['mail']         = $('input[name=mail]').val();
          data['location']     = $('input[name=location]').val();
          data['native_place'] = $('input[name=native_place]').val();
          data['emer_name']    = $('input[name=emer_name]').val();
          data['emer_phone']   = $('input[name=emer_phone]').val();
          doRequest(_this,url,'PUT',data);
      });
      var major_html = {};
      function renderAcademyWithMajor(data) {
          var academy_html = getRenderHtml(data['academy_data'],'<option value="{id}">{name}</option>');
          academy_html = '<option value="0">请选择学院</option>' + academy_html;
          $('select[name=academy_id]').html(academy_html);
          $.each(data['major_data'], function (i, item) {
              major_html[item[0]['academy_id']] = '<option value="0">请选择专业</option>' + getRenderHtml(item,'<option value="{id}">{name}</option>');
          });
          <% unless @user.academy_id.blank? %>
          $('select[name=academy_id]').val("<%= @user.academy_id %>");
          $('select[name=major_id]').html(major_html[<%= @user.academy_id %>] || '');
          $('select[name=major_id]').val("<%= @user.major_id %>");
          <% end %>
      }
      $('select[name=academy_id]').change(function () {
          var academy_id = $(this).val();
          $('select[name=major_id]').html(major_html[academy_id] || '');
      });
      doRequest('','/admin/my/get_academy_with_majors','GET',{},renderAcademyWithMajor);
  </script>
<% end %>